package book;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.sql.SQLException;

import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

public class Registration {       // 注册界面
    JFrame f2;
    JPanel p2;
    JButton b2;
    JLabel l3, l4, l5;
    JTextField t2;
    JPasswordField s2, s3;

    Registration() {
        f2 = new JFrame("注册");
        p2 = new JPanel();
        b2 = new JButton("确认");
        l3 = new JLabel("学号：");
        t2 = new JTextField(18);
        l4 = new JLabel("密码：");
        l5 = new JLabel("确认密码：");
        s2 = new JPasswordField(18);
        s3 = new JPasswordField(18);

        p2.setLayout(null);
        l3.setBounds(120, 120, 40, 20);
        t2.setBounds(160, 120, 150, 20);
        b2.setBounds(180, 240, 100, 20);
        l4.setBounds(120, 160, 40, 20);
        s2.setBounds(160, 160, 150, 20);
        l5.setBounds(95,200,80,20);
        s3.setBounds(160,200,150,20);

        f2.add(p2);
        p2.add(l3);
        p2.add(t2);
        p2.add(l4);
        p2.add(l5);
        p2.add(s2);
        p2.add(s3);
        p2.add(b2);
        f2.setVisible(true);
        f2.setBounds(600, 250, 500, 500);
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Boolean flag;
                String id = t2.getText();
                String password = new String(s2.getPassword());
                String CheckPassWord = new String(s3.getPassword());
                if(!password.equals(CheckPassWord))
                {
                    JOptionPane.showMessageDialog(null, "错误", "密码错误", JOptionPane.ERROR_MESSAGE);
                }
                else
                {
                     flag = SearchUser.search(id);

                     if( flag ) {
                         try {
                             InnerUser.inner(id,password);
                             f2.dispose();
                         } catch (SQLException ex) {
                             throw new RuntimeException(ex);
                         }
                     };

                }
            }
        });
        f2.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

}
